<template>
    <div class="list-page">
        <div class="list-page-header list-page-header-flex">
            <div class="flex-left">
                <el-button type="primary" @click="add">新增</el-button>
                <el-button type="primary" @click="edit">编辑</el-button>
                <el-popconfirm title="确认删除？" confirmButtonText="确定" cancelButtonText="取消" @confirm="del">
                    <template #reference>
                        <el-button>删除</el-button>
                    </template>
                </el-popconfirm>
            </div>
            <div class="flex-right">
                <el-input v-model="query.projectName" class="search-input-class" placeholder="项目名称" />
                <el-button type="primary" @click="loadData">查询</el-button>
                <el-button @click="reset">重置</el-button>
            </div>
        </div>
        <div class="list-content">
            <el-table
                ref="datagrid"
                :data="table.data"
                border
                style="width: 100%; height: 100%"
                @row-click="handleClickTableRow"
                @selection-change="handleSelectionChange"
            >
                <el-table-column type="selection" width="55" />
                <el-table-column prop="projectName" label="申请项目" show-overflow-tooltip />
                <el-table-column prop="reason" label="报废原因" show-overflow-tooltip />
                <el-table-column prop="createBy" label="申请人" width="120" />
                <el-table-column
                    prop="createTime"
                    label="申请日期"
                    :formatter="formatUtil.gridDateFormat"
                    width="140"
                />
                <el-table-column prop="stateName" label="状态" width="180" show-overflow-tooltip />
                <el-table-column prop="handlerName" label="当前处理人" width="100" show-overflow-tooltip />
                <el-table-column fixed="right" label="操作" width="140">
                    <template #default="scope">
                        <el-popconfirm
                            v-if="flowUtil.showSubmit(scope.row)"
                            title="确认提交审批吗？"
                            confirmButtonText="确定"
                            cancelButtonText="再考虑一下"
                            @confirm="flowSub(scope.row)"
                        >
                            <template #reference>
                                <el-button type="primary" size="small">提交</el-button>
                            </template>
                        </el-popconfirm>
                        <el-popconfirm
                            v-if="flowUtil.showRevoke(scope.row)"
                            title="确认撤回吗？"
                            confirmButtonText="确定"
                            cancelButtonText="再考虑一下"
                            @confirm="flowRevoke(scope.row)"
                        >
                            <template #reference>
                                <el-button type="warning" size="small">撤回</el-button>
                            </template>
                        </el-popconfirm>
                        <el-button
                            v-if="flowUtil.showApproval(scope.row)"
                            type="primary"
                            size="small"
                            @click="review(scope.row)"
                        >
                            审核
                        </el-button>
                        <el-button size="small" @click.stop="review(scope.row)">查看</el-button>
                    </template>
                </el-table-column>
            </el-table>
        </div>
        <div class="list-footer">
            <el-pagination
                background
                @size-change="handleSizeChange"
                @current-change="handleCurrentChange"
                :current-page="table.pagination.currentPage"
                :page-sizes="table.pagination.pageSizes"
                :page-size="table.pagination.pageSize"
                layout="total, sizes, prev, pager, next, jumper"
                :total="table.pagination.total"
            ></el-pagination>
        </div>
        <transition name="slide-fade">
            <div v-if="showInfoWindow" class="list-form-modal">
                <DeviceDiscardForm :id="selectedId" @close="closeInfoWindow"></DeviceDiscardForm>
            </div>
        </transition>
    </div>
</template>
<script setup>
import { ref, reactive, onMounted } from 'vue'
import { ElNotification } from 'element-plus'
import DeviceDiscardForm from './form/DeviceDiscardForm.vue'
import http from '@/http.js'
import formatUtil from '@/utils/format.js'
import flowUtil from '@/utils/flow.js'

const datagrid = ref(null)

const query = reactive({
    projectName: ''
})

const table = reactive({
    data: [],
    selected: [],
    pagination: {
        currentPage: 1,
        pageSizes: [10, 20, 50, 100],
        pageSize: 10,
        total: 0
    }
})

const selectedId = ref('')

const showInfoWindow = ref(false)

const handleSelectionChange = (val) => {
    table.selected = val
}

const handleSizeChange = (val) => {
    table.pagination.pageSize = val
    loadData()
}

const handleCurrentChange = (val) => {
    table.pagination.currentPage = val
    loadData()
}

const handleClickTableRow = (row) => {
    datagrid.value.clearSelection()
    datagrid.value.toggleRowSelection(row)
}

const loadData = () => {
    let params = {
        projectName: query.projectName,
        page: table.pagination.currentPage,
        pageSize: table.pagination.pageSize
    }
    http.fetchForm({
        url: '/yunying/deviceDiscard/queryGrid',
        data: params
    }).then((res) => {
        table.data = res.data.grid
        table.pagination.total = res.data.total
    })
}

const reset = () => {
    query.projectName = ''
    loadData()
}

const add = () => {
    selectedId.value = ''
    showInfoWindow.value = true
}

const edit = () => {
    selectedId.value = ''
    let selected = table.selected
    if (selected.length == 0) {
        ElNotification({
            title: '提示',
            message: '请选择要编辑的工程',
            type: 'info'
        })
        return
    }
    selectedId.value = selected[0].id
    showInfoWindow.value = true
}

const del = () => {
    let selected = table.selected
    if (selected.length == 0) {
        ElNotification({
            title: '提示',
            message: '请选择要删除的工程',
            type: 'info'
        })
        return
    }
    http.fetchForm({
        url: '/yunying/deviceDiscard/deleteById',
        method: 'post',
        data: {
            id: selected[0].id
        }
    }).then((res) => {
        ElNotification({
            title: '提示',
            message: '删除成功',
            type: 'success'
        })
        loadData()
    })
}

const closeInfoWindow = () => {
    showInfoWindow.value = false
    loadData()
}

// 提交审批
const flowSub = (row) => {
    flowUtil.doSubmit(row, () => {
        loadData()
    })
}

// 撤回
const flowRevoke = (row) => {
    flowUtil.doRevoke(row, () => {
        loadData()
    })
}

const review = (row) => {
    selectedId.value = row.id
    showInfoWindow.value = true
}

// mounted
onMounted(() => {
    loadData()
})
</script>
<style scoped>
@import '@/assets/css/list-page.css';
</style>
